import type { CwindMenus } from '@/models/cwind_menus.model';

import { MenuManagementListDto } from '../dto';

/*
 * @Author: C.
 * @Date: 2025-05-13 18:44:54
 */
export function initializeTree(
  resource: CwindMenus[],
  parentId: any = null,
): MenuManagementListDto[] {
  const tree: MenuManagementListDto[] = [];

  for (const item of resource) {
    const target: MenuManagementListDto = new MenuManagementListDto();
    target.component = item.component;
    target.name = item.name;
    target.path = item.path;
    target.menu_id = item.menu_id;
    target.type = item.type;
    target.status = item.status;
    target.pid = item.pid;
    target.permission = item.permission;

    target.meta = {
      title: item.title,
      icon: item.icon,
      affix_tab: item.affix_tab,
      order: item.order,
      badge: item.badge,
      badge_type: item.badge_type,
      badge_variants: item.badge_variants,
    };
    if (target.pid === parentId) {
      const children = initializeTree(resource, item.menu_id);
      if (children.length > 0) {
        target.children = children;
      }
      tree.push(target);
    }
  }
  return tree;
}
